package leetcode.code1726;

import java.util.HashMap;
import java.util.Map;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

/**
 * 执行用时：207 ms, 在所有 Java 提交中击败了41.96%的用户
 *
 * 内存消耗：69 MB, 在所有 Java 提交中击败了44.06%的用户
 *
 * 通过测试用例：37 / 37
 */
public class Solution2 implements IDebug {

	public int tupleSameProduct(int[] nums) {
		int ans = 0;
		Map<Integer, Integer> hash = new HashMap<>();
		int len = nums.length;
		for (int i = 0; i < len - 1; i++) {
			for (int j = i + 1; j < len; j++) {
				int mul = nums[i] * nums[j];
				int count = hash.getOrDefault(mul, 0);
				hash.put(mul, count + 1);
			}
		}
		for (Integer mul : hash.keySet()) {
			int count = hash.get(mul);
			if (count > 1) {
				ans += (count * (count - 1)) << 2;// cn2 (n-1)*n/2*4*2;
			}
		}
		return ans;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(8, this.tupleSameProduct(HelpDebug.str2array3("[2,3,4,6]")));

	}

	@Override
	public void debug2() {
		HelpDebug.compare(16, this.tupleSameProduct(HelpDebug.str2array3("[1,2,4,5,10]")));
	}

	@Override
	public void debug3() {
		HelpDebug.compare(40, this.tupleSameProduct(HelpDebug.str2array3("[2,3,4,6,8,12]")));
	}

	@Override
	public void debug4() {
		HelpDebug.compare(0, this.tupleSameProduct(HelpDebug.str2array3("[2,3,5,7]")));
	}

	public static void main(String[] args) {
		Solution2 so = new Solution2();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
